﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Chromos.ActionEngine;
using Chromos.AIEngine;

namespace Chromos.Actors
{
	public class Player : Actor
	{
		public Player(Game1 game, Vector2 position, Vector2 velocity, ContentManager content, bool isStatic)
			: base(game, position, velocity, content, isStatic)
		{
			boundingInflate = new Vector2(-7, 0);
			updateBoundingBox(true);
		}

		protected override void initAIEngine()
		{
			aiEngine = new PlayerAIEngine(this, "normal", 15);
		}

		protected override void initActorFields()
		{
			this.mass = 50;
			this.health = 200;
		}

		public override Projectile createProjectiles(int i)
		{
			switch (i)
			{
				case 0:
					return leftGunP();
				case 1:
					return rightGunP();
				default:
					return null;
			}
		}
		private Projectile leftGunP()
		{
			Vector2 v = new Vector2(0, 0);
			v.X = -400;
			Vector2 p = new Vector2(this.position.X, this.position.Y);
			p.X -= 20;
			return new Bullet(this.game, p, v, this.game.Content, false, this, 0, null);
		}
		private Projectile rightGunP()
		{
			Vector2 v = new Vector2(0, 0);
			v.X = 400;
			Vector2 p = new Vector2(this.position.X, this.position.Y);
			p.X += 20;
			return new Bullet(this.game, p, v, this.game.Content, false, this, 0, null);
		}

		public override void addActions()
		{
			// Idle
			IdleAction m1 = new IdleAction(this, content, Vector2.Zero, ForceType.ForceInstant, 50);
			m1.addImageToLoad(@"Sprites/Player/StickStanding");

			// StickLeft
			WalkAction m2 = new WalkAction(this, content, new Vector2(-160, 0), ForceType.ForceInstant, 50, FACING.LEFT);
			m2.addImageToLoad(@"Sprites/Player/Move/Move/StickMoveLeft1");
			m2.addImageToLoad(@"Sprites/Player/Move/Move/StickMoveLeft2");
			m2.addImageToLoad(@"Sprites/Player/Move/Move/StickMoveLeft3");
			m2.addImageToLoad(@"Sprites/Player/Move/Move/StickMoveLeft4");

			// StickRight
			WalkAction m3 = new WalkAction(this, content, new Vector2(160, 0), ForceType.ForceInstant, 50, FACING.RIGHT);
			m3.addImageToLoad(@"Sprites/Player/Move/Move/StickMoveRight1");
			m3.addImageToLoad(@"Sprites/Player/Move/Move/StickMoveRight2");
			m3.addImageToLoad(@"Sprites/Player/Move/Move/StickMoveRight3");
			m3.addImageToLoad(@"Sprites/Player/Move/Move/StickMoveRight4");

            // Jumping
            JumpAction jump = new JumpAction(this, content, new Vector2(0, -180*2), ForceType.ForceInstant, 50);
            jump.addImageToLoad(@"Sprites/Player/StickStanding");

			// Gun attack
			AttackAction gunLeft = new AttackAction(this, content, new Vector2(0, 0), ForceType.ForceInstant, 50, createProjectiles, 0, 0, 0, 400, 0, 0, 0, null, null, FACING.LEFT, false);
			gunLeft.addImageToLoad(@"Sprites/Player/Attack/GunAttacks/GunLeftFire1");
			gunLeft.addImageToLoad(@"Sprites/Player/Attack/GunAttacks/GunLeftFire2");
			gunLeft.addImageToLoad(@"Sprites/Player/Attack/GunAttacks/GunLeftFire3");
			gunLeft.addImageToLoad(@"Sprites/Player/Attack/GunAttacks/GunLeftFire4");
			AttackAction gunRight = new AttackAction(this, content, new Vector2(0, 0), ForceType.ForceInstant, 50, createProjectiles, 0, 0, 0, 400, 0, 0, 0, null, null, FACING.RIGHT, false);
			gunRight.addImageToLoad(@"Sprites/Player/Attack/GunAttacks/GunRightFire1");
			gunRight.addImageToLoad(@"Sprites/Player/Attack/GunAttacks/GunRightFire2");
			gunRight.addImageToLoad(@"Sprites/Player/Attack/GunAttacks/GunRightFire3");
			gunRight.addImageToLoad(@"Sprites/Player/Attack/GunAttacks/GunRightFire4");

			// Light sword
			AttackAction lightSwordL = new AttackAction(this, content, Vector2.Zero, ForceType.ForceInstant, 50, null, 100, 0, 50, 300, 0, 0, 0, null, null, FACING.LEFT, false);
			lightSwordL.addImageToLoad(@"Sprites/Player/Attack/WhiteAbilities/LightSword/LightSwordLeft1");
			lightSwordL.addImageToLoad(@"Sprites/Player/Attack/WhiteAbilities/LightSword/LightSwordLeft2");
			lightSwordL.addImageToLoad(@"Sprites/Player/Attack/WhiteAbilities/LightSword/LightSwordLeft3");
			lightSwordL.addImageToLoad(@"Sprites/Player/Attack/WhiteAbilities/LightSword/LightSwordLeft4");
			lightSwordL.addImageToLoad(@"Sprites/Player/Attack/WhiteAbilities/LightSword/LightSwordLeft5");
			AttackAction lightSwordR = new AttackAction(this, content, Vector2.Zero, ForceType.ForceInstant, 50, null, 100, 0, 50, 300, 0, 0, 0, null, null, FACING.RIGHT, false);
			lightSwordR.addImageToLoad(@"Sprites/Player/Attack/WhiteAbilities/LightSword/LightSwordRight1");
			lightSwordR.addImageToLoad(@"Sprites/Player/Attack/WhiteAbilities/LightSword/LightSwordRight2");
			lightSwordR.addImageToLoad(@"Sprites/Player/Attack/WhiteAbilities/LightSword/LightSwordRight3");
			lightSwordR.addImageToLoad(@"Sprites/Player/Attack/WhiteAbilities/LightSword/LightSwordRight4");
			lightSwordR.addImageToLoad(@"Sprites/Player/Attack/WhiteAbilities/LightSword/LightSwordRight5");

            // Death
            DeathAction death = new DeathAction(this, content, Vector2.Zero, ForceType.ForceInstant, 50);
            death.addImageToLoad(@"Sprites/Player/Death/Death01");
            death.addImageToLoad(@"Sprites/Player/Death/Death02");
            death.addImageToLoad(@"Sprites/Player/Death/Death03");
            death.addImageToLoad(@"Sprites/Player/Death/Death04");
            death.addImageToLoad(@"Sprites/Player/Death/Death05");
            death.addImageToLoad(@"Sprites/Player/Death/Death06");
            death.addImageToLoad(@"Sprites/Player/Death/Death07");
            death.addImageToLoad(@"Sprites/Player/Death/Death08");
            death.addImageToLoad(@"Sprites/Player/Death/Death09");
            death.addImageToLoad(@"Sprites/Player/Death/Death10");

			actionEngine.addIdle(m1);
			actionEngine.addMovement(m2);
			actionEngine.addMovement(m3);
            actionEngine.addMovement(jump);
			actionEngine.addAttack(gunLeft);
			actionEngine.addAttack(gunRight);
			actionEngine.addAttack(lightSwordL);
			actionEngine.addAttack(lightSwordR);
            actionEngine.addDeath(death);
		}

		public override void collidedProjectile(Projectile p)
		{
			if (p.firingActor != this)
			{
				this.health -= p.power;
				p.health -= p.power;
				// Simulated exchange of energy (bounding box size is representitive of mass)
				//this.velocity += p.velocity / ((float)this.mass) * ((float)p.mass);
			}
		}

		public void attack(int p)
		{
			this.health -= p;
		}
	}
}
